package com.iespring.service.app.search;

import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.promotion.po.MallCoupon;
import com.iespring.bean.admin.promotion.po.MallCouponExample;
import com.iespring.bean.admin.user.entity.*;
import com.iespring.bean.app.search.Keyword;
import com.iespring.bean.app.search.MallKeyword;
import com.iespring.bean.app.search.MallKeywordExample;
import com.iespring.bean.app.search.SearchData;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.mapper.admin.MallCouponMapper;
import com.iespring.mapper.admin.MallGoodsMapper;
import com.iespring.mapper.admin.MallSearchHistoryMapper;
import com.iespring.mapper.app.MallKeywordMapper;
import com.iespring.util.UserInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author:ZhangShuPeng
 * @Date:2023/01/02/13:21
 * @Description:
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    MallKeywordMapper mallKeywordMapper;

    @Autowired
    MallSearchHistoryMapper mallSearchHistoryMapper;

    @Autowired
    MallCouponMapper mallCouponMapper;

    @Autowired
    MallGoodsMapper mallGoodsMapper;

    @Override
    public BaseRespVo index() {

        MallKeywordExample mallKeywordExample = new MallKeywordExample();
        MallKeywordExample.Criteria criteria = mallKeywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsDefaultEqualTo(true);
        criteria.andIsHotEqualTo(true);
        List<MallKeyword> mallKeywords = mallKeywordMapper.selectByExample(mallKeywordExample);
        PageInfo<MallKeyword> mallKeywordPageInfo = new PageInfo<>(mallKeywords);
        List<MallKeyword> list = mallKeywordPageInfo.getList();
        MallKeyword defaultKeyword = list.get(0);

        List<MallSearchHistory> mallSearchHistories = mallSearchHistoryMapper.selectAllHistory();
        List<Keyword> historyKeywordList = new ArrayList<>();
        for (MallSearchHistory mallSearchHistory : mallSearchHistories) {
            String name = mallSearchHistory.getKeyword();
            Keyword keyword = new Keyword(name);
            historyKeywordList.add(keyword);
        }

        MallKeywordExample mallKeywordExample1 = new MallKeywordExample();
        MallKeywordExample.Criteria criteria1 = mallKeywordExample1.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria1.andIsDefaultEqualTo(false);
        criteria1.andIsHotEqualTo(true);
        List<MallKeyword> hotKeywordList = mallKeywordMapper.selectByExample(mallKeywordExample1);

        SearchData data = new SearchData(defaultKeyword, historyKeywordList, hotKeywordList);

        BaseRespVo baseRespVo = BaseRespVo.ok(data);

        return baseRespVo;
    }

    @Override
    public BaseRespVo helper(String keyword) {

        List<String> data = new ArrayList<>();

        MallSearchHistoryExample mallSearchHistoryExample = new MallSearchHistoryExample();
        MallSearchHistoryExample.Criteria criteria = mallSearchHistoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andKeywordLike("%" + keyword + "%");
        List<MallSearchHistory> mallSearchHistories = mallSearchHistoryMapper.selectByExample(mallSearchHistoryExample);

        MallCouponExample mallCouponExample = new MallCouponExample();
        MallCouponExample.Criteria criteria1 = mallCouponExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria1.andNameLike("%" + keyword + "%");
        List<MallCoupon> mallCoupons = mallCouponMapper.selectByExample(mallCouponExample);

        MallGoodsExample mallGoodsExample = new MallGoodsExample();
        MallGoodsExample.Criteria criteria2 = mallGoodsExample.createCriteria();
        criteria2.andDeletedEqualTo(false);
        criteria2.andNameLike("%" + keyword + "%");
        List<MallGoods> mallGoods = mallGoodsMapper.selectByExample(mallGoodsExample);

        for (MallGoods mallGood : mallGoods) {
            String name = mallGood.getName();
            data.add(name);
        }

        for (MallCoupon mallCoupon : mallCoupons) {
            String name = mallCoupon.getName();
            data.add(name);
        }

        for (MallSearchHistory mallSearchHistory : mallSearchHistories) {
            String keyword1 = mallSearchHistory.getKeyword();
            data.add(keyword1);
        }

        System.out.println(data);

        BaseRespVo baseRespVo = BaseRespVo.ok(data);

        return baseRespVo;
    }

    @Override
    public BaseRespVo clearhistory() {

        List<MallSearchHistory> mallSearchHistories = mallSearchHistoryMapper.selectAllHistory();
        for (MallSearchHistory mallSearchHistory : mallSearchHistories) {
            mallSearchHistory.setDeleted(true);
            mallSearchHistoryMapper.updateSearchHistory(mallSearchHistory);
        }
        BaseRespVo baseRespVo = BaseRespVo.ok();

        return baseRespVo;

    }

    @Override
    public void insertKeyword(String keyword) {

        MallSearchHistory mallSearchHistory = new MallSearchHistory();
        MallUser user = UserInfoUtils.getUser();
        mallSearchHistory.setUserId(user.getId());
        mallSearchHistory.setKeyword(keyword);
        mallSearchHistory.setFrom("wx");
        mallSearchHistory.setAddTime(new Date());
        mallSearchHistory.setUpdateTime(new Date());
        mallSearchHistory.setDeleted(false);

        mallSearchHistoryMapper.insert(mallSearchHistory);
    }


}
